<template>
  <div>
    <el-breadcrumb separator=">" style="font-size: 20px">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item><a href="/">添加管理员用户</a></el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>
    <el-card style="width: 1000px;height: 100%;margin: 0 auto;padding: 30px;
    background-color: rgba(255,255,255,0.3)">
      <div style="width: 900px;position: relative;left: 30px">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="名称" prop="realName">
            <el-input v-model="ruleForm.realName"></el-input>
          </el-form-item>
          <el-form-item label="身份证号码" prop="cardId">
            <el-input v-model="ruleForm.cardId"></el-input>
          </el-form-item>
          <el-form-item label="注册账号" prop="userName">
            <el-input v-model="ruleForm.userName"></el-input>
          </el-form-item>
          <el-form-item label="注册密码" prop="password">
            <el-input type="password" v-model="ruleForm.password"></el-input>
          </el-form-item>
          <el-form-item label="电话号码" prop="phone">
            <el-input v-model="ruleForm.phone"></el-input>
          </el-form-item>
          <el-form-item style="margin: 0 130px;position: relative;right: 30px">
            <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-card>

  </div>
</template>

<script>

export default {
  data() {
    return {
      ruleForm: {
        realName: '',
        cardId: '',
        userName:'',
        password:'',
        phone: '',
        carId: '',
      },

      rules: {
        realName: [
          {required: true, message: '姓名', trigger: 'blur'},
          {min: 1, max: 15, message: '长度在 1 到 15 个字符', trigger: 'blur'}
        ],
        cardId: [
          {required: true, message: '身份证号码', trigger: 'blur'},
          {min: 18, max: 18, message: '输入正确的身份证号码', trigger: 'blur'}
        ],
        userName: [
          {required: true, message: '管理员账号', trigger: 'blur'},
          {min: 7, max: 10, message: '账号请满足7-10位', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '管理员密码', trigger: 'blur'},
          {min: 7, max: 10, message: '密码请满足7-10位', trigger: 'blur'}
        ],
        carId: [
          {required: true, message: '请输入排序序号', trigger: 'blur'},
        ],
        phone: [
          {required: true, message: '手机号', trigger: 'blur'},
          {min: 11, max: 11, message: '输入正确的手机号', trigger: 'blur'}
        ],

      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:8080/admin/AddAdminUser';
          console.log('url='+url,this.ruleForm);
          // let formData = this.qs.stringify(this.ruleForm);

          console.log(localStorage.getItem('jwt'))
          this.axios
              .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
              .post(url,this.ruleForm).then((response)=>{
            let responseBody = response.data;
            console.log(responseBody);
            if (responseBody.code == 200){
              this.$message({
                showClose: true,
                message: '添加管理员账户成功！',
                type: 'success'
              });
              this.resetForm(formName);
            }else {
              this.$message({
                showClose: true,
                message: responseBody.message,
                type: 'error'
              });
            }
          })

        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
      resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>
